//
//  DoneViewController.m
//  WayToHome
//
//  Created by A-01 on 2020/6/23.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "DoneViewController.h"

@interface DoneViewController ()

@property (nonatomic, weak)UILabel * titleLab;
@property (nonatomic, weak)UILabel * scoreLab;
@property (nonatomic, weak)UIButton * restartBtn;
@property (nonatomic, weak)UIButton * backBtn;
@property (nonatomic, strong)NSArray * resArray;
@property (nonatomic, strong)NSArray * homeArray;

@end

@implementation DoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bottomImageView.hidden = YES;
    self.backButton.hidden = YES;
    [self setupUI];
}

- (void)setupUI{
            
    UILabel * titleLab = [UILabel new];
    
    titleLab.textColor = [UIColor colorWithHexString:@"0AB966"];
    titleLab.font = [UIFont boldSystemFontOfSize:24];
    titleLab.textAlignment = NSTextAlignmentCenter;
            
    self.titleLab = titleLab;
            
    [self.view addSubview:self.titleLab];
    
    
    UILabel * scoreLab = [UILabel new];
        
    scoreLab.text = self.score;
    scoreLab.textColor = [UIColor colorWithHexString:@"0AB966"];
    scoreLab.font = [UIFont boldSystemFontOfSize:55];
    scoreLab.textAlignment = NSTextAlignmentCenter;
            
    self.scoreLab = scoreLab;
            
    [self.view addSubview:self.scoreLab];
    
    
    UIButton * restartButton = [[UIButton alloc] init];
    
    [restartButton setBackgroundColor:[UIColor colorWithHexString:@"0AB966"]];
    [restartButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [restartButton addTarget:self action:@selector(restartButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    restartButton.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    restartButton.layer.cornerRadius = 24;
    restartButton.layer.masksToBounds = YES;
    
    self.restartBtn = restartButton;
    
    [self.view addSubview:self.restartBtn];
    
    
    UIButton * backBtn = [[UIButton alloc] init];
    
    [backBtn setBackgroundColor:[UIColor colorWithHexString:@"8BDBA8"]];
    [backBtn setTitle:@"Back" forState:UIControlStateNormal];
    [backBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    backBtn.layer.cornerRadius = 24;
    backBtn.layer.masksToBounds = YES;
    
    self.backBtn = backBtn;
    
    [self.view addSubview:self.backBtn];
        
    switch (self.type) {
        case 0:
            
            self.titleLab.text = @"Game Over";
            [self.restartBtn setTitle:@"Restart" forState:UIControlStateNormal];
            break;
        case 1:
            
            self.titleLab.text = @"Well Done";
            if ([self.index isEqualToString:@"20"]) {
               
                [self.restartBtn setTitle:@"Done" forState:UIControlStateNormal];
            }else{
                
                [self.restartBtn setTitle:@"Next" forState:UIControlStateNormal];
            }
            
            break;
        default:
            break;
    }
    
}


- (void)restartButtonClicked{
    
    if ([self.index isEqualToString:@"20"]) {
       
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    if ([self.restartBtn.titleLabel.text isEqualToString:@"Restart"]) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(backWithType:WithResArr:WithHome:)])
        {

            [self.delegate backWithType:@"back" WithResArr:nil WithHome:0];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(backWithType:WithResArr:WithHome:)])
        {
            int count = [self.index intValue];
            int res = [self.homeArray[count] intValue];
            NSArray * arr = self.resArray[count];
            [self.delegate backWithType:@"next" WithResArr:arr WithHome:res];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


- (void)backButtonClicked{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];

    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(70);
    }];
    
    [self.scoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(10);
    }];
    
    [self.restartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(105);
        make.width.mas_equalTo(245);
        make.height.mas_equalTo(48);
    }];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.height.mas_equalTo(self.restartBtn);
        make.top.mas_equalTo(self.restartBtn.mas_bottom).offset(25);
    }];
}


- (NSArray *)resArray{
    
    if (!_resArray) {
        
        _resArray = @[@[@3,@13,@23,@33,@34],@[@4,@14,@24,@23,@33],@[@9,@19,@29,@39,@38],@[@5,@15,@14,@24,@34],@[@6,@16,@26,@25,@24,@34],@[@6,@16,@26,@25,@24,@34,@33],@[@3,@13,@14,@15,@16,@26,@36],@[@6,@16,@26,@27,@28,@29,@39],@[@4,@14,@15,@16,@26,@36,@35,@34],@[@0,@10,@20,@21,@22,@23,@24,@34],@[@4,@14,@15,@16,@26,@36,@35,@34,@33],@[@9,@19,@29,@28,@27,@26,@25,@35],@[@1,@11,@12,@22,@23,@33,@34],@[@5,@15,@16,@26,@36,@35,@34,@33],@[@1,@11,@12,@13,@23,@24,@25,@26,@36,@37],@[@7,@17,@18,@28,@38,@37,@36,@35,@34,@33],@[@8,@18,@28,@38,@37,@36,@35,@34,@33,@32],@[@8,@18,@17,@16,@15,@14,@13,@23,@33,@34,@35,@36],@[@3,@13,@12,@11,@21,@31,@32,@33,@34,@35,@36,@37],@[@6,@16,@15,@14,@13,@12,@22,@32,@33,@34,@35,@36,@37]];
    }

    return _resArray;
}


- (NSArray *)homeArray{
    
    if (!_homeArray) {
        
        _homeArray = @[@44,@43,@48,@44,@44,@43,@46,@49,@44,@44,@43,@45,@44,@43,@47,@43,@42,@46,@47,@47];
    }
    
    return _homeArray;
}

@end
